/*
	Compresor Gzip
    Copyright (C) 2012 Darío López Padial 
    @bukosabino

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>
*/

/**
 @file vectorbits.cpp
 @brief Implementación de la Clase Vector Dinámico de char
 @author Dario Lopez Padial
*/

#include <iostream>
#include "vectordinamico.h"

using namespace std;

VD::VD ( const VD & v ) {
	_tamanio = v._tamanio;
	_vector = new unsigned char [v._tamanio];
	for ( unsigned int i=0; i < v._tamanio; i++ ) {
		_vector [i] = v._vector [i];
	}
}


void VD::Resize ( unsigned int n ) {
	unsigned char *aux;
  aux = new unsigned char [n];
  if ( _tamanio <= n ) {
   	for( unsigned int i=0; i<Size( ); i++ ) {
   	  aux [ i ] = Get ( i );
   	}
	}
	else {
	  for ( unsigned int i=0; i<n; i++ ) {
			aux [ i ] = Get ( i );
    }
	}
  delete []_vector;
 	_vector=aux;
 	_tamanio=n;
}

void VD::Add(unsigned char c) { 
	Resize(Size()+1);
  Set(Size()-1,c);
}

void VD::Leer( std::istream & is ) {
  Resize(0);
	int tam;
	is.seekg(0,ios::end);
	tam=is.tellg();
	is.seekg(0,ios::beg);
	Resize(tam);
	for(int i=0; i<tam; i++) {
		(*this).Set (i, is.get() );
	}
}

void const VD::Escribir() {
	cout << endl;
  for ( unsigned int i=0; i<Size(); i++ )
  	cout << Get( i );
	cout << endl;
}

VD & VD :: operator = ( const VD & v ){
	if ( this != &v ) {
		delete [] _vector;
		_vector = new unsigned char [ v._tamanio ];
		_tamanio = v._tamanio;
		for( unsigned int i=0; i<_tamanio; i++ ) {
			_vector [i] = v._vector [i];
		}
	}
	return * this;
}
